subroutine run(ie, t1, t2, r1, r2,fij, fab, fia, & 
          fae, fmi, fme, iijkl, wmnij, iijka, &
          iabij, iiabj, wmbej, icore, &
          maxcore, escf, eccsd, eccsdt) 
use mod_ri
use mod_iop
use mod_size
use mod_orbit
use mod_ioff
implicit none
integer prec2, isd2, igpu2, totgpu2
common/precsoc/prec2, isd2, igpu2, totgpu2
!
integer, intent(inout) :: maxcore
real*8, intent(inout) :: escf, eccsd, eccsdt
real*4, intent(in) :: ie(*), fij(*), fab(*), fia(*)
real*4, intent(in) :: fae(*), fmi(*), fme(*)
real*4, intent(in) :: t1(*), t2(*), r1(*), r2(*)
real*4, intent(in) :: iijka(*), iabij(*), iijkl(*)
real*4, intent(in) :: wmnij(*), wmbej(*)
real*4, intent(in) :: iiabj(*), icore(*)
real*4, allocatable,device :: ig(:)
!
integer nsize 
integer i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10 
integer i11, i12, i13, i14, i15, i16, i17, i18, i19
integer i20, i21, i22
character*8 frivvaa, frivvbb, frivoaa, frivobb, friooaa, frioobb
integer, external :: irpdso, idsymsoc
if(prec==8) then 
  frivvaa = 'RIVVAA__'
  frivvbb = 'RIVVBB__'
  frivoaa = 'RIVOAA__'
  frivobb = 'RIVOBB__'
  friooaa = 'RIOOAA__'
  frioobb = 'RIOOBB__'
elseif(prec==4) then 
  frivvaa = 'RIVVAAS_'
  frivvbb = 'RIVVBBS_'
  frivoaa = 'RIVOAAS_'
  frivobb = 'RIVOBBS_'
  friooaa = 'RIOOAAS_'
  frioobb = 'RIOOBBS_'
endif

!
prec2=prec; isd2=isd; igpu2=igpu; totgpu2=totgpu
!write(6,*)'totgpu', totgpu, totgpu2
if(prec==8) then 
call load(ie, fij, fab, fia, & 
          iijkl, iijka, iabij, iiabj, &
          icore, escf)
elseif(prec==4) then 
call load_s(ie, fij, fab, fia, & 
          iijkl, iijka, iabij, iiabj, &
          icore, escf)
endif
write(6,*)'finished load'
!
igpuall = 0
maxcore = maxcore - nioiri_vv*isd - nioiri_vo*isd
if(totgpu==0.or.(totgpu==1.and.igpuall==0)) then
  write(6,*)'calc ccsd with large scal'
  i0 = 1 
  i1 = i0 + nioiri_vv*isd 
  i2 = i1 + nioiri_vo*isd 
  i3 = i2 + nioiri_oo*isd 
  call getallso(icore(i3), 1, frivvaa)
  call icompso(icore(i3), icore(i0-1+ioiri_vv(1)), naux8, zorb, 1, 1, vrta, vrta, 1, 2, 1)
  call getallso(icore(i3), 1, frivvbb)
  call icompso(icore(i3), icore(i0-1+ioiri_vv(2)), naux8, zorb, 1, 1, vrtb, vrtb, 1, 2, 1)

  call getallso(icore(i1-1+ioiri_vo(1)), 1, frivoaa)
  call getallso(icore(i1-1+ioiri_vo(2)), 1, frivobb)

  call getallso(icore(i2-1+ioiri_oo(1)), 1, friooaa)
  call getallso(icore(i2-1+ioiri_oo(2)), 1, frioobb)

  call srccsd(ie, fij, fab, fia,  & 
            iijkl, iijka, iabij, iiabj,  &
            t1, t2, r1, r2, fae, fmi, fme, wmnij, wmbej, icore(i3),&
            maxcore, escf, eccsd, icore(i0), icore(i1), icore(i2))
! call srccsd_io(ie, fij, fab, fia,  & 
!           iijkl, iijka, iabij,   &
!           t1, t2, r1, r2, fae, fmi, fme, wmnij, icore(i3),&
!           maxcore, escf, eccsd, icore(i0), icore(i1), icore(i2))
elseif(igpuall==1.and.totgpu==1) then
  write(6,*)'!!!!!!!!!!'
  write(6,*)'all integrals were stored in GPU'
!  igpuabcd=0
  nsize = igpusize
  allocate(ig(nsize))
  i0  = 1                    !ie
  i1  = i0  + (nva+noa+nvb+nob)*isd   !fij
  i2  = i1  + nifoo*isd       !fab
  i3  = i2  + nifvv*isd       !fia
  i4  = i3  + nifvo*isd       !iijkl
  i5  = i4  + nioi1*isd       !iijka
  i6  = i5  + nioi2*isd       !iabij
  i7  = i6  + nioi3*isd       !iiabj
  i8  = i7                    
  i9  = i8  + nioi4*isd       !t1
  i10 = i9  + nifvo*isd       !t2
  i11 = i10 + nioi3*isd       !r1
  i12 = i11 + nifvo*isd       !r2
  i13 = i12 + nioi3*isd       !fae
  i14 = i13 + nifvv*isd       !fmi
  i15 = i14 + nifoo*isd       !fme 
  i16 = i15 + nifvo*isd       !wmnij 
  i17 = i16 + nioi1*isd       !wmbej 
  i18 = i17 + nioi4*isd       !icore
  i19 = i18
  i20 = i19 + nioiri_vv*isd
  i21 = i20 + nioiri_vo*isd
  i22 = i21 + nioiri_oo*isd
  nsize = max(idsymsoc(1, vrta, vrta, 2, naux8, zorb, 1), &
              idsymsoc(1, vrtb, vrtb, 2, naux8, zorb, 1))*isd
  igpuleft = igpusize-i19-max(nsize, nvvoo*isd*5)
  call myicopy_g(ie,  ig(i0), nva+noa+nvb+nob)
  call myicopy_g(fij, ig(i1), nifoo)
  call myicopy_g(fab, ig(i2), nifvv)
  call myicopy_g(fia, ig(i3), nifvo)
  call myicopy_g(iijkl, ig(i4), nioi1)
  call myicopy_g(iijka, ig(i5), nioi2)
  call myicopy_g(iabij, ig(i6), nioi3)
  call myicopy_g(iiabj, ig(i7), nioi4)

  nsize = max(idsymsoc(1, vrta, vrta, 1, naux8, zorb, 1), & 
              idsymsoc(1, vrtb, vrtb, 1, naux8, zorb, 1))*isd
  call getallso(icore, 1, frivvaa)
  call icompso(icore, icore(nsize+1), naux8, zorb, 1, 1, vrta, vrta, 1, 2, 1)
  call myicopy_g(icore(nsize+1), ig(i19), nioiri_vv1)
  call getallso(icore, 1, frivvbb)
  call icompso(icore, icore(nsize+1), naux8, zorb, 1, 1, vrtb, vrtb, 1, 2, 1)
  call myicopy_g(icore(nsize+1), ig(i19-1+ioiri_vv(2)), nioiri_vv2)

  call getallso(icore, 1, frivoaa)
  call myicopy_g(icore, ig(i20), nioiri_vo1)
  call getallso(icore, 1, frivobb)
  call myicopy_g(icore, ig(i20-1+ioiri_vo(2)), nioiri_vo2)

  call getallso(icore, 1, friooaa)
  call myicopy_g(icore, ig(i21), nioiri_oo1)
  call getallso(icore, 1, frioobb)
  call myicopy_g(icore, ig(i21-1+ioiri_oo(2)), nioiri_oo2)

  write(6,*)'begin calculation in gpu'
  call srccsdg(ig(i0), ig(i1), ig(i2), ig(i3),  & 
            ig(i4), ig(i5), ig(i6), ig(i7),  &
            ig(i9), ig(i10), ig(i11), ig(i12), ig(i13), ig(i14), ig(i15),&
            ig(i16), ig(i17), ig(i22),&
            maxcore, escf, eccsd, r1, r2, t1, t2, icore, &
            ig(i19), ig(i20), ig(i21))
endif  
 
return
end 
